package com.doodle.common.utils;

import android.graphics.Bitmap;
import android.graphics.Color;

public class DominantColors {
    public static final int PROCESS_TPYE_MUSIC = 0;
    public static final int PROCESS_TPYE_WEBKIT = 1;

    public DominantColors() {
    }

    private static int getColor(int color) {
        // ARGB_8888
        int red = (color >> 16) & 0x000000ff;
        int green = (color >> 8) & 0x000000ff;
        int blue = color & 0x000000ff;
        return Color.argb(255, red, green, blue);
    }

        /*
        *    for example:
        *    (1) PROCESS_TPYE_MUSIC 
        *    int[] bgColorArray = new int[1]; 
        *    Bitmap dstBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.xxxxx);
        *    DominantColors.process(dstBitmap, bgColorArray, DominantColors.);
        *    
        *    (2) PROCESS_TPYE_WEBKIT 
        *    int[] bgColorArray = new int[1];
        *    Bitmap dstBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.xxxxx);
        *    DominantColors.processIcon(dstBitmap, bgColorArray);
        *    
        */
    public static boolean process(Bitmap bitmap, int[] bgColor, int type) {
        int width, height;
        if (bitmap != null && bgColor != null) {
            width = bitmap.getWidth();
            height = bitmap.getHeight();

            if (type == PROCESS_TPYE_MUSIC) {
                bgColor[0] = nativeProcess(bitmap, width, height);
            } else if (type == PROCESS_TPYE_WEBKIT) {
                bgColor[0] = nativeProcessICO(bitmap, width, height);
            }
            bgColor[0] = getColor(bgColor[0]);
            return true;
        }
        return false;
    }

    public static void fillColor(Bitmap bitmap, int color) {
        int width, height;

        if (bitmap != null) {
            width = bitmap.getWidth();
            height = bitmap.getHeight();
            nativeFillColor(bitmap, width, height, color);
        }
    }

    public static void topEdge(Bitmap bitmap) {
        int width, height;

        if (bitmap != null) {
            width = bitmap.getWidth();
            height = bitmap.getHeight();
            nativeTopEdge(bitmap, width, height);
        }
    }

    public static void getDarkenLightColorRatio(Bitmap bitmap, int[] darkenColorRatio, int[] lightColorRatio) {
        int width, height;

        if (bitmap != null && darkenColorRatio != null && lightColorRatio != null) {
            width = bitmap.getWidth();
            height = bitmap.getHeight();
            nativeGetDarkenLightColorRatio(bitmap, width, height, darkenColorRatio, lightColorRatio);
        }
    }

    protected static native int nativeProcessICO(Bitmap bitmap, int width, int height);
    protected static native int nativeProcess(Bitmap bitmap, int width, int height);
    protected static native void nativeFillColor(Bitmap bitmap, int width, int height, int color);
    protected static native void nativeTopEdge(Bitmap bitmap, int width, int height);
    protected static native void nativeGetDarkenLightColorRatio(Bitmap bitmap, int width, int height, int[] darkenColorRatio, int[] lightColorRatio);

    static {
        System.loadLibrary("DominantColors");
    }
}
